package com.mega.timestop.item;

import com.mega.timestop.EventUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.RunGameLoop;
import net.minecraft.client.resources.I18n;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumAction;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.Timer;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;

import java.io.IOException;

public class ItemTimeClock extends Item {
    public ItemTimeClock() {
        setRegistryName("timeclock");
        setUnlocalizedName("timeclock");
        setCreativeTab(CreativeTabs.MISC);
        setMaxStackSize(1);
    }

    public EnumAction getItemUseAction(ItemStack stack) {
        return EnumAction.NONE;
    }

    @Override
    public int getMaxItemUseDuration(ItemStack p_77626_1_) {
        return 72000;
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
        Minecraft mc = Minecraft.getMinecraft();
        //mc.entityRenderer.loadEntityShader(EventUtil.SHADER_ENTITY);
        //mc.timer.tickLength = 0;
        EventUtil.timeStop = true;
        ItemStack itemstack = playerIn.getHeldItem(handIn);
        playerIn.setActiveHand(handIn);
        mc.addScheduledTask(() -> {
            while (EventUtil.timeStop)
                try {
                    RunGameLoop.runGameLoop();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        });
        return new ActionResult<>(EnumActionResult.SUCCESS, itemstack);
    }

    @Override
    public void onPlayerStoppedUsing(ItemStack p_77615_1_, World p_77615_2_, EntityLivingBase p_77615_3_, int p_77615_4_) {
        Minecraft mc = Minecraft.getMinecraft();
        //mc.entityRenderer.stopUseShader();
        mc.timer = new Timer(20F);
        EventUtil.timeStop = false;
    }
}
